package com.kaoqin.code.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @ClassName: AuthorUtils
 * @Description: TODO
 * @author: Janus
 * @date: 2017-7-27 上午11:03:35
 */
public class AuthorUtils {
	/**
	 * @Title: getIpAddr
	 * @Description: 获取来访者Ip地址
	 * @param request
	 * @return: String
	 * @date: 2017-7-27 上午11:03:59
	 * @author: Janus
	 */
	public static String getIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("X-Real-IP");
		if (ip != null && !"".equals(ip) && !"unknown".equalsIgnoreCase(ip)) {
			return ip;
		}
		ip = request.getHeader("Proxy-Client-IP");
		if (ip != null && !"".equals(ip) && !"unknown".equalsIgnoreCase(ip)) {
			return ip;
		}
		ip = request.getHeader("WL-Proxy-Client-IP");
		if (ip != null && !"".equals(ip) && !"unknown".equalsIgnoreCase(ip)) {
			return ip;
		}

		ip = request.getHeader("X-Forwarded-For");
		if (ip != null && !"".equals(ip) && !"unknown".equalsIgnoreCase(ip)) {
			// 多次反向代理后会有多个IP值，第一个为真实IP。
			int index = ip.indexOf(',');
			if (index != -1) {
				return ip.substring(0, index);
			} else {
				return ip;
			}
		} else {
			return request.getRemoteAddr();
		}
	}
	/**
	* @Title: getToken 
	* @Description: 获取Token
	* @param @param request
	* @param @return
	* @param @throws Exception    设定文件 
	* @return String    返回类型 
	* @throws 
	* @author Janus
	* @Date 2017年9月5日 下午5:30:18
	 */
	public static String getToken(HttpServletRequest request) throws Exception {
		HttpSession session = request.getSession();
		Object tokenObj = session.getAttribute("token");
		String token="";
		if(tokenObj!=null) {
			token=tokenObj.toString();
		}
		return token;
	}
	public static DESUtils getDESToken() throws Exception {
		return new DESUtils("token");
	}
}
